﻿using OurMind.Controls.Connectors;
using OurMind.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace OurMind.Controls
{
    ///<summary>连接符的生成工厂。</summary>
    internal class ConnectorManager<T> where T : BaseConnector, new()
    {
        public Dictionary<MindConnect, T> _map = new Dictionary<MindConnect, T>();

        public T CreateConnector(MindConnect mindConnect)
        {
            var ins =  new T();
            _map.Add(mindConnect, ins);
            return ins;
        }

        public T GetConnector(MindConnect mindConnect)
        {
            return _map[mindConnect];
        }

        public void ArrangeConnector(T connector, Orientation orientation, Point startPoint, Point endPoint)
        {
            Rect rect = GetConnectorRect(startPoint, endPoint);
            connector.Width = rect.Width;
            connector.Height = rect.Height;

            var direction = GetConnectorDirectionEnum(orientation, startPoint, endPoint);
            connector.Direction = direction;
            connector.Arrange(rect);
        }

        private static Rect GetConnectorRect(Point startPoint, Point endPoint)
        {
            var rawRect = new Rect(startPoint, endPoint);
            var marginOffset = new Vector(BaseConnector.LineMargin, BaseConnector.LineMargin);
            var newLeftTop = rawRect.TopLeft - marginOffset;
            var newRightBottom = rawRect.BottomRight+marginOffset;

            return new Rect(newLeftTop, newRightBottom);
        }

        private ConnectorDirectionEnum GetConnectorDirectionEnum(Orientation orientation, Point startPoint, Point endPoint)
        {
            switch (orientation)
            {
                case Orientation.Horizontal:
                    Point left;
                    Point right;

                    if (startPoint.X <= endPoint.X)
                    {
                        left = startPoint;
                        right = endPoint;
                    }
                    else
                    {
                        right = startPoint;
                        left = endPoint;
                    }

                    if (left.Y <= right.Y)
                    {
                        return ConnectorDirectionEnum.LeftTopToRightBottom;
                    }
                    else
                    {
                        return ConnectorDirectionEnum.LeftBottomToRightTop;
                    }

                case Orientation.Vertical:
                    Point top;
                    Point bottom;

                    if (startPoint.Y <= endPoint.Y)
                    {
                        top = startPoint;
                        bottom = endPoint;
                    }
                    else
                    {
                        bottom = startPoint;
                        top = endPoint;
                    }

                    if (top.X <= bottom.X)
                    {
                        return ConnectorDirectionEnum.LeftTopToRightBottom;
                    }
                    else
                    {
                        return ConnectorDirectionEnum.LeftBottomToRightTop;
                    }

                default:
                    return ConnectorDirectionEnum.LeftBottomToRightTop;
            }
        }

    }
}
